package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.TagTextView;

import static cn.xfdzx.android.apps.shop.apps.Constants.SHOPID_69_NO;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：
 */
public class OrderDetailGoodAdapter extends RecyclerView.Adapter<OrderDetailGoodAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<OrderListBeen.DataBean.CartInfoBean> list;
    private Context context;
    public boolean is_open = false;
    private String shopId;


    public OrderDetailGoodAdapter(Context context, List<OrderListBeen.DataBean.CartInfoBean> list,String shopId) {
        this.list = list;
        this.context = context;
        this.shopId = shopId;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_confirm_order_goods, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

//        holder. ivOrderGoods.setImageResource(R.drawable.image_tudou);


        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        //        RequestOptions.bitmapTransform(new CircleCrop())
        Glide.with(context).load(list.get(position).getProductInfo().getImage()).apply(options.transform(new CenterCrop(), roundedCorners)).into((holder.ivOrderGoods));
        holder.tvOrderGoodsName.setText(list.get(position).getProductInfo().getStore_name());
        holder.tvOrderGoodsTotalOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        if (UtilsData.getPreference(context, "activity").getBoolean("is_69")&&!SHOPID_69_NO.contains(shopId)) {
            if (list.get(position).getGive_goods() == 1) {//减哪一件
                holder.tvOrderGoodsName.setSingleTagAndContent("任减一件", list.get(position).getProductInfo().getStore_name(), R.drawable.icon_activity_bg, "#FFFFFFFF");
                holder.tvGoodMian.setText("¥0.00x1");
                holder.tvGoodMian.setVisibility(View.VISIBLE);
                if (Double.parseDouble(list.get(position).getDiscountPrice()) == 0) {
                    holder.tvOrderGoodsTotalPrice.setText("¥" + Utils.mul(Double.parseDouble(list.get(position).getMarketPrice()) ,(list.get(position).getCart_num() - 1)));
                    holder.tvOrderGoodsNum.setText("¥" + list.get(position).getMarketPrice() + "x" + (list.get(position).getCart_num() - 1) + "");
                    holder.tvOrderGoodsTotalOldPrice.setVisibility(View.VISIBLE);
                    holder.tvOrderGoodsTotalOldPrice.setText("¥" + Utils.mul(Double.parseDouble(list.get(position).getMarketPrice()) , list.get(position).getCart_num()));

                } else {
                    holder.tvOrderGoodsTotalPrice.setText("¥" +Utils.mul(Double.parseDouble(list.get(position).getDiscountPrice()) , (list.get(position).getCart_num() - 1)) );
                    holder.tvOrderGoodsNum.setText("¥" + list.get(position).getDiscountPrice() + "x" + (list.get(position).getCart_num() - 1) + "");
                    holder.tvOrderGoodsTotalOldPrice.setVisibility(View.VISIBLE);
                    holder.tvOrderGoodsTotalOldPrice.setText("¥" +Utils.mul( Double.parseDouble(list.get(position).getMarketPrice()), list.get(position).getCart_num()));
                }
                if (list.get(position).getCart_num() < 2) {
                    holder.tvOrderGoodsNum.setVisibility(View.INVISIBLE);
                } else {
                    holder.tvOrderGoodsNum.setVisibility(View.VISIBLE);
                }
            } else {
                holder.tvOrderGoodsNum.setVisibility(View.VISIBLE);
                holder.tvGoodMian.setVisibility(View.GONE);
                holder.tvOrderGoodsName.setText(list.get(position).getProductInfo().getStore_name());
                if (Double.parseDouble(list.get(position).getDiscountPrice()) == 0) {
                    holder.tvOrderGoodsTotalPrice.setText("¥" + +Utils.mul(Double.parseDouble(list.get(position).getMarketPrice()) , list.get(position).getCart_num()));
                    holder.tvOrderGoodsNum.setText("¥" + list.get(position).getMarketPrice() + "x" + list.get(position).getCart_num() + "");
                    holder.tvOrderGoodsTotalOldPrice.setVisibility(View.GONE);
                } else {
                    holder.tvOrderGoodsTotalPrice.setText("¥" + Utils.mul(Double.parseDouble(list.get(position).getDiscountPrice()),list.get(position).getCart_num()));
                    holder.tvOrderGoodsNum.setText("¥" + list.get(position).getDiscountPrice() + "x" + list.get(position).getCart_num() + "");
                    holder.tvOrderGoodsTotalOldPrice.setVisibility(View.VISIBLE);
                    holder.tvOrderGoodsTotalOldPrice.setText("¥" + Utils.mul(Double.parseDouble(list.get(position).getMarketPrice()) , list.get(position).getCart_num()));
                }
            }
        } else {
            holder.tvOrderGoodsNum.setVisibility(View.VISIBLE);
            holder.tvGoodMian.setVisibility(View.GONE);
            holder.tvOrderGoodsName.setText(list.get(position).getProductInfo().getStore_name());
            if (Double.parseDouble(list.get(position).getDiscountPrice()) == 0) {
                holder.tvOrderGoodsTotalPrice.setText("¥" +Utils.mul(Double.parseDouble(list.get(position).getMarketPrice()) , list.get(position).getCart_num()) );
                holder.tvOrderGoodsNum.setText("¥" + list.get(position).getMarketPrice() + "x" + list.get(position).getCart_num() + "");
                holder.tvOrderGoodsTotalOldPrice.setVisibility(View.GONE);
            } else {
                holder.tvOrderGoodsTotalPrice.setText("¥" +Utils.mul( Double.parseDouble(list.get(position).getDiscountPrice()) , list.get(position).getCart_num()));
                holder.tvOrderGoodsNum.setText("¥" + list.get(position).getDiscountPrice() + "x" + list.get(position).getCart_num() + "");
                holder.tvOrderGoodsTotalOldPrice.setVisibility(View.VISIBLE);
                holder.tvOrderGoodsTotalOldPrice.setText("¥" + Utils.mul(Double.parseDouble(list.get(position).getMarketPrice()) , list.get(position).getCart_num()));
            }
        }

    }


    @Override
    public int getItemCount() {
        if (list.size() > 2) {
            if (!is_open) {
                return 2;
            } else {
                return list.size();
            }
        }
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.iv_order_goods)
        ImageView ivOrderGoods;
        @BindView(R.id.tv_order_goods_name)
        TagTextView tvOrderGoodsName;
        @BindView(R.id.tv_order_goods_num)
        TextView tvOrderGoodsNum;
        @BindView(R.id.tv_good_mian)
        TextView tvGoodMian;
        @BindView(R.id.tv_order_goods_total_price)
        TextView tvOrderGoodsTotalPrice;
        @BindView(R.id.tv_order_goods_total_old_price)
        TextView tvOrderGoodsTotalOldPrice;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
